const moment = require('moment')
const messageInfo = require('../utils/messageInfo.js')
const { mysqlApi } = require('../model/db')
// 查询所有分类
exports.CategoryList = async (req, res) => {
    const level = req.params.level
    const oneSql = `SELECT * FROM categorys WHERE pid = '0';`
    const oneResult = await mysqlApi(oneSql)
    let twoResult = null
    let threeResult = null
    if (level > 1) {
        for (let i = 0; i < oneResult.length; i++) {
            if (oneResult[i].cid == 0) continue;
            const twoSql = `SELECT * FROM categorys WHERE pid = '${oneResult[i].cid}';`
            twoResult = await mysqlApi(twoSql)
            if (level > 2) {
                for (let j = 0; j < twoResult.length; j++) {
                    const threeSql = `SELECT * FROM categorys WHERE pid = '${twoResult[j].cid}';`
                    threeResult = await mysqlApi(threeSql)
                    twoResult[j].children = threeResult
                }
            }
            oneResult[i].children = twoResult
        }
    }
    res.status(200).json(messageInfo(oneResult, 200, "查询成功"))
}

// 添加分类
exports.CategoryAdd = async (req, res) => {
    const addSql = `INSERT INTO categorys SET pid="${req.body.pid}",create_time="${moment().format('YYYY-MM-DD HH:mm:ss')}",title="${req.body.title}",imgsrc=${"'" + req.body.imgsrc + "'" || null},is_show="${req.body.is_show || 1}";`
    try {
        const result = await mysqlApi(addSql)
        res.status(200).json(messageInfo(result, '201', '添加成功'))
    } catch (error) {
        res.status(400).json(messageInfo(error, '400', '添加失败'))
    }
}

// 删除分类
exports.CategoryDelete = async (req, res) => {
    const selectSql = `SELECT * FROM categorys WHERE pid="${req.params.cid}";`
    const selectResult = await mysqlApi(selectSql)
    if (selectResult.length == 0) {
        const deleteSql = `DELETE FROM categorys WHERE cid="${req.params.cid}";`
        try {
            const deleteResult = await mysqlApi(deleteSql)
            res.status(200).json(messageInfo(deleteResult, '204', '删除成功'))
        } catch (error) {
            res.status(400).json(messageInfo(error, '400', '删除失败'))
        }
    } else {
        res.status(400).json(messageInfo(null, '400', '清空子分类后再次尝试'))
    }
}

// 修改分类状态
exports.CategoryEditStatus = async (req, res) => {
    const selectSql = `SELECT * FROM categorys WHERE cid="${req.params.cid}";`
    const selectResult = await mysqlApi(selectSql)
    if (selectResult.length > 0) {
        const editSql = `UPDATE categorys SET is_show="${req.params.value}" WHERE cid=${req.params.cid};`
        try {
            const editResult = await mysqlApi(editSql)
            res.status(200).json(messageInfo(editResult, '200', '修改成功'))
        } catch (error) {
            res.status(400).json(messageInfo(error, '400', '修改失败'))
        }
    } else {
        res.status(400).json(messageInfo(null, '400', '没有找到用户'))
    }
}

// 根据id查找分类
exports.CategoryById = async (req, res) => {
    const selectSql = `SELECT * FROM categorys WHERE cid="${req.params.cid}";`
    const selectResult = (await mysqlApi(selectSql))[0]
    if (selectResult) {
        res.status(200).json(messageInfo(selectResult, '200', '查询成功'))
    } else {
        res.status(400).json(messageInfo(null, '400', '没有找到用户'))
    }
}


// 修改分类
exports.CategoryEdit = async (req, res) => {
    console.log(req.body);
    const selectSql = `SELECT * FROM categorys WHERE cid="${req.params.cid}";`
    const selectResult = await mysqlApi(selectSql)
    if (selectResult.length > 0) {
        const editSql = `UPDATE categorys SET title="${req.body.title}",pid="${req.body.pid}",imgsrc=${"'" + req.body.imgsrc + "'" || null} WHERE cid="${req.params.cid}";`
        try {
            const editResult = await mysqlApi(editSql)
            res.status(200).json(messageInfo(editResult, '200', '修改成功'))
        } catch (error) {
            res.status(400).json(messageInfo(error, '400', '修改失败'))
        }
    } else {
        res.status(400).json(messageInfo(null, '400', '没有找到用户'))
    }
}